package com.itest.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itest.entity.Case;

import com.itest.dao.ServiceMapper;
import com.itest.service.HttpClientUtils;
import com.itest.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
/*
 * 魏巍 赵金龙
 * */

@Controller
public class PostServiceController {


    @Autowired
    private ServiceMapper dao;


    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/doService/postService.do",method = RequestMethod.POST)
    public CommonResult doPost(

            @RequestBody Case cas
    ) throws IOException {
        CommonResult cr = new CommonResult();
        String res =null;



        if(cas.getUrl()!=null&& cas.getBody()!=null){
            JSONObject json = JSON.parseObject(cas.getBody());
            res = HttpClientUtils.doPost(cas.getUrl(),json);
        }

        if(res!= null){
            cr.setCode(0);//代表成功
            cr.setMsg("获取接口返回值成功");
            cr.setData(res);
        }else {
            cr.setCode(1);
            cr.setMsg("获取接口返回值失败");
            cr.setData("");
        }


        Case intable = dao.selectCaseByUrl(cas);
        if(intable==null){
            Case cas2 = cas;
            dao.insertCase(cas2);
        }

        return cr;
    }
}